<?php

namespace App\Services\ApplyGoldProductReselling;

use App\Http\Controllers\Api\V1\Requests\FormRequest;

class ApplyGoldProductResellingRequest extends FormRequest
{
	public function rules()
	{
		$this->verify();
		
		return [];
	}
	
	public function verify()
	{
		$record = $this->record;
		
		if ($record->isClosed()) {
			fail('当前记录已关闭');
		} elseif (! $record->isPaid()) {
			fail('当前记录未支付');
		} elseif ($this->isLastRound()) {
			fail('最后一轮请申请超额返');
		} elseif (! $this->isGoldBeansEnough()) {
			fail('可用金豆不足');
		} elseif ($record->hasAppliedToResell()) {
			fail('不可重复申请寄售');
		} elseif ($record->hasAppliedToSplit()) {
			fail('当前记录已申请超额返');
		} elseif ($record->hasAppliedToTake()) {
			fail('当前记录已申请发货');
		}
	}
	
	protected function isLastRound()
	{
		return $this->record->goldProduct->isLastRound();
	}
	
	protected function isGoldBeansEnough()
	{
		return $this->user()->isGoldBeansEnough($this->record->record_beans);
	}
}
